package com.itheima.mall.web.sms;

import cn.hutool.core.convert.Convert;
import com.itheima.mall.pojo.sms.SmsHomeAdvertise;
import com.itheima.mall.service.sms.HomeAdvertiseService;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/home/advertise")
public class HomeAdvertiseController {
    @Autowired
    HomeAdvertiseService advertiseService;
    @PostMapping("/create")
    public R<String> createAdvertise(@RequestBody SmsHomeAdvertise smsHomeAdvertise){
        advertiseService.createAdvertise(smsHomeAdvertise);
        return R.succeed(null,"创建成功");
    }
    @PostMapping("/delete")
    public R<String> deleteAdvertise(@RequestParam("ids") int[] ids){
        advertiseService.deleteAdvertise(ids);
        return R.succeed(null,"删除成功");
    }
    @GetMapping("list")
    public R<PageResult<SmsHomeAdvertise>> getList(@RequestParam("endTime")String endTime,
                                                   @RequestParam("name")String name,
                                                   @RequestParam("pageNum")Integer pageNum,
                                                   @RequestParam("pageSize")Integer pageSize,
                                                   @RequestParam("type")Integer type){
        PageResult<SmsHomeAdvertise> list = advertiseService.getList(endTime, name, pageNum, pageSize, type);
        return R.succeed(list,"查询成功");
    }
    @PostMapping("update/status/{id}")
    public R<String> updateStatus(@PathVariable Integer id,@RequestParam("status")Integer status){
        advertiseService.updateStatus(id,status);
        return R.succeed(null,"修改成功");
    }
    @PostMapping("update/{id}")
    public R<String> updateAdvertise(@RequestBody SmsHomeAdvertise smsHomeAdvertise,@PathVariable Integer id){
     smsHomeAdvertise.setId(Convert.toLong(id));
        advertiseService.updateAdvertise(smsHomeAdvertise);
        return R.succeed(null,"更新成功");
    }
    @GetMapping("{id}")
    public R<SmsHomeAdvertise> getAdvertise(@PathVariable Integer id){
        SmsHomeAdvertise advertise = advertiseService.getAdvertise(id);
        return R.succeed(advertise,"查询成功");

    }
}
